package com.ftg.exam.manage.service;

import com.ftg.exam.manage.mapper.ImportMapper;
import com.ftg.exam.manage.util.ExcelUtil;
import com.ftg.exam.manage.util.ImportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author zwl
 * excel服务
 * Created on 2019/8/13 11:36
 */
@Service
public class ImportService implements ImportServiceImpl {
    @Autowired
    ImportMapper importMapper;

    /**
     * excel单项选择
     * @param file
     * @return
     */
    @Override
    public String OneFile(MultipartFile file) {
        String result = "";
        //创建处理EXCEL的类
        ExcelUtil readExcel = new ExcelUtil();
        //解析excel，获取上传的事件单
        List<Map<String, Object>> userList = readExcel.getExcelInfo(file);
        //至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
        for(Map<String, Object> user:userList){
            int ret = importMapper.OneInsert(user.get("difficulty").toString(), user.get("question").toString(), user.get("A").toString(),user.get("B").toString(),user.get("C").toString(),user.get("D").toString(),user.get("answer").toString(),user.get("analysis").toString());

            if(ret == 0){
                result = "插入数据库失败";
            }
        }
        if(userList != null && !userList.isEmpty()){
            result = "上传成功";
        }else{
            result = "上传失败";
        }
        return result;
    }

    /**
     * 多项选择
     * @param file
     * @return
     */
    @Override
    public String MultipleFile(MultipartFile file){
        String result = "";
        //创建处理EXCEL的类
        ExcelUtil readExcel = new ExcelUtil();
        //解析excel，获取上传的事件单
        List<Map<String, Object>> userList = readExcel.getExcelInfo(file);
        //至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
        for(Map<String, Object> user:userList){
            int ret = importMapper.MultipleInsert(user.get("difficulty").toString(), user.get("question").toString(), user.get("A").toString(),user.get("B").toString(),user.get("C").toString(),user.get("D").toString(),user.get("answer").toString(),user.get("analysis").toString());

            if(ret == 0){
                result = "插入数据库失败";
            }
        }
        if(userList != null && !userList.isEmpty()){
            result = "上传成功";
        }else{
            result = "上传失败";
        }
        return result;
    }
}
